package com.dianning.web.action.user;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Company;
import com.dianning.bean.Info;
import com.dianning.bean.Message;
import com.dianning.bean.Profile;
import com.dianning.bean.Report;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.comm.Util;
import com.dianning.service.InfoService;
import com.dianning.service.MessageService;
import com.dianning.service.ProfileService;
import com.dianning.service.ReportService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.action.BaseAction;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class ReportAction extends BaseAction implements SessionAware, UserSessionAware{

	private static final Log logger = LogFactory.getLog(ReportAction.class);

	private UserInfoService userInfoService;
	private ReportService reportService;
	private InfoService infoService;
	private ProfileService profileService;


	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	public ReportService getReportService() {
		return reportService;
	}

	public void setReportService(ReportService reportService) {
		this.reportService = reportService;
	}
	
	public InfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}
	
	public ProfileService getProfileService() {
		return profileService;
	}

	public void setProfileService(ProfileService profileService) {
		this.profileService = profileService;
	}
	
	private Info info;
	private UserInfo userInfo;
	
	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}
	
	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}


	private UserSession userSession;
	private Map session;
	
	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private long id;
	private String reporttype;
	private String reason;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public String getReporttype() {
		return reporttype;
	}

	public void setReporttype(String reporttype) {
		this.reporttype = reporttype;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	
	public String add(){
		this.setInfo(this.getInfoService().findInfoById(this.id));
		if(this.getInfo() == null){
			return "infodisable";
		}else{
			if(this.getInfo().getValidated() == 2){
				return "infodisable";
			}
		}
		return "add";
	}
	
	public String doadd(){
		Report r = this.getReportService().findReportByReporttypeOid("info", this.id);
		if(r == null){
			r = new Report();
			r.setOid(this.id);
			r.setType(this.reporttype);
			r.setReporttype("info");
			r.setIsnew(1);
			r.setNum(1);
			r.setUids(String.valueOf(userSession.getUid()));
			r.setReason(this.reason);
			r.setAddTime(new Date());
			r.setUpTime(new Date());
		}else{
			if(r.getNum()>5){
				r.setNum(r.getNum()+1);
				r.setIsnew(1);
				r.setUpTime(new Date());
			}else{
				r.setType(r.getType()+","+this.reporttype);
				r.setUids(r.getUids()+","+String.valueOf(userSession.getUid()));
				r.setReason(r.getReason()+"|"+this.reason);
				r.setNum(r.getNum()+1);
				r.setIsnew(1);
				r.setUpTime(new Date());
			}
		}
		this.getReportService().createReport(r);
		this.setInfo(this.getInfoService().findInfoById(this.id));
		if(this.getInfo() == null){
			return "infodisable";
		}else{
			if(this.getInfo().getValidated() == 2){
				return "infodisable";
			}
		}
		this.getSession().put("code", "report_create");
		return "add";
	}
	
	public String profile(){
		this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.id));
		return "profile";
	}
	
	public String doprofile(){
		Report r = this.getReportService().findReportByReporttypeOid("profile", this.id);
		if(r == null){
			r = new Report();
			r.setOid(this.id);
			r.setType(this.reporttype);
			r.setReporttype("profile");
			r.setIsnew(1);
			r.setNum(1);
			r.setUids(String.valueOf(userSession.getUid()));
			r.setReason(this.reason);
			r.setAddTime(new Date());
			r.setUpTime(new Date());
		}else{
			if(r.getNum()>5){
				r.setNum(r.getNum()+1);
				r.setIsnew(1);
				r.setUpTime(new Date());
			}else{
				r.setType(r.getType()+","+this.reporttype);
				r.setUids(r.getUids()+","+String.valueOf(userSession.getUid()));
				r.setReason(r.getReason()+"|"+this.reason);
				r.setNum(r.getNum()+1);
				r.setIsnew(1);
				r.setUpTime(new Date());
			}
		}
		this.getReportService().createReport(r);
		this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.id));
		this.getSession().put("code", "report_create");
		return "profile";
	}


}
